package tt.dz.appserver.service.business.imp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.ext.AppVersionDao;
import tt.dz.appserver.entity.model.AppVersion;
import tt.dz.appserver.service.business.AppVersionService;
import tt.dz.appserver.util.date.DateUtil;
import tt.dz.appserver.util.enums.Response;

/**
 * App版本管理实现类
 * @author USER001
 *
 */

public class AppVersionServiceImpl implements AppVersionService {

	@Resource
	private AppVersionDao appVersionDao;
	
	@Override
	public JSONObject appversionadd(String versioncode, Integer ostype, String description, String releasetime,
			String supportosver, String downloadurl) throws AvroRemoteException {
		// TODO Auto-generated method stub
		AppVersion appVersion = new AppVersion();
		JSONObject resultJsonObject = new JSONObject();
		Integer validflag = 1;		
		
		appVersion.setVersion_code(versioncode);
		appVersion.setOs_type(ostype);
		appVersion.setDescription(description);
		appVersion.setRelease_time(DateUtil.parseStringToDateTime(releasetime));
		appVersion.setSupport_os_version(supportosver);
		appVersion.setDownload_url(downloadurl);
		appVersion.setValid_flag(validflag);
		
		appVersionDao.addAppVersion(appVersion);
		
		resultJsonObject.put("versioncode", versioncode);
		resultJsonObject.put("ostype", ostype);
		resultJsonObject.put("description", description);
		resultJsonObject.put("releasetime", releasetime);
		resultJsonObject.put("supportosver", supportosver);
		resultJsonObject.put("downloadurl", downloadurl);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject appversiondel(String versioncode, Integer ostype) throws AvroRemoteException {
		// TODO Auto-generated method stub
		AppVersion appVersion = new AppVersion();
		JSONObject resultJsonObject = new JSONObject();
		Integer validflag = 0;		
		
		appVersion.setVersion_code(versioncode);
		appVersion.setOs_type(ostype);
		appVersion.setValid_flag(validflag);
		
		appVersionDao.delAppVersion(appVersion);
		
		resultJsonObject.put("versioncode", versioncode);
		resultJsonObject.put("ostype", ostype);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject appversionupdate(String versioncode, Integer ostype, String description, String releasetime,
			String supportosver, String downloadurl) throws AvroRemoteException {
		// TODO Auto-generated method stub
		AppVersion appVersion = new AppVersion();
		JSONObject resultJsonObject = new JSONObject();
		Integer validflag = 1;		
		
		appVersion.setVersion_code(versioncode);
		appVersion.setOs_type(ostype);
		appVersion.setDescription(description);
		appVersion.setRelease_time(DateUtil.parseStringToDateTime(releasetime));
		appVersion.setSupport_os_version(supportosver);
		appVersion.setDownload_url(downloadurl);
		appVersion.setValid_flag(validflag);
		
		appVersionDao.updateAppVersion(appVersion);
		
		resultJsonObject.put("versioncode", versioncode);
		resultJsonObject.put("ostype", ostype);
		resultJsonObject.put("description", description);
		resultJsonObject.put("releasetime", releasetime);
		resultJsonObject.put("supportosver", supportosver);
		resultJsonObject.put("downloadurl", downloadurl);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public List<AppVersion> appversionselect(Integer ostype) throws AvroRemoteException {
		// TODO Auto-generated method stub
		
		return appVersionDao.findAppVersionValid(ostype);

	}

}
